package IO.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 通过网络传输一张图片
 *
 * 客户端获取服务端的一张图片
 * @author 15594
 */
public class SocketTest1 {
    public static void main(String[] args)  {
        server();
    }
    private static void server() {

        ServerSocket serverSocket = null;
        Socket accept = null;
        BufferedInputStream bis =null;
        OutputStream outputStream =null;
        try {
            bis = new BufferedInputStream(new FileInputStream("welcome.jpg"));

            serverSocket = new ServerSocket(8899);

            accept = serverSocket.accept();

            System.out.println("创建连接的用户ip："+accept.getInetAddress());

            outputStream = accept.getOutputStream();

            byte [] bytes = new byte[1024];
            int len = 0;
            while ((len = bis.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){

                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis!=null){

                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            server();
        }
    }
}

class Client1{
    public static void main(String[] args) {
        client();
    }
    private static void client() {
        Socket socket = null;
        InputStream inputStream = null;
        BufferedOutputStream bos = null;
        try {
            InetAddress inet = InetAddress.getByName("192.168.8.66");
            socket = new Socket(inet, 8899);
            inputStream = socket.getInputStream();
            bos = new BufferedOutputStream(new FileOutputStream("来自服务器的图片.jpg"));
            byte [] bytes = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos!=null){

                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
